unit LibDmoConnection;

interface

uses
  SysUtils, Classes, DB, ADODB;

type
  TdmoConnection = class(TDataModule)
    adoConn: TADOConnection;
    adoQry: TADOQuery;
    qryFuncionalidade: TADOQuery;
    procedure DataModuleCreate(Sender: TObject);
    procedure DataModuleDestroy(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure Connect;
    procedure Disconnect;
    function GetConnection: TADOConnection;
  end;

var
  dmoConnection: TdmoConnection;

implementation

uses clsConfig, Dialogs;

{$R *.dfm}
{ TdmoConnection }

procedure TdmoConnection.Connect;
begin
  try
    try
      adoConn.Close;
      adoConn.ConnectionString := Config.Connetion.ConnectionString;
      adoConn.Open();
    except
      on E: Exception do
      begin
        raise Exception.Create
          ('Erro ao conectar o banco de dados.'#13'Mensagem Original: ' +
          E.Message + #13#13 + 'Local TdmoConnection::Connect');
      end;
    end;
  finally
    Config.Connetion.Connected := adoConn.Connected;
  end;
end;

procedure TdmoConnection.DataModuleCreate(Sender: TObject);
begin
  Connect();
end;

procedure TdmoConnection.DataModuleDestroy(Sender: TObject);
begin
  Disconnect();
end;

procedure TdmoConnection.Disconnect;
begin
  adoConn.Close;
end;

function TdmoConnection.GetConnection: TADOConnection;
begin
  Result := adoConn;
end;

end.
